﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Graph;
using HTML;
using Crawler;
using System.Diagnostics;

namespace CrawlerGUI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void txt_url_keyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (String.IsNullOrEmpty(txt_url.Text) || txt_url.Text.Equals("about:blank"))
                {
                    MessageBox.Show("Enter a valid URL.");
                    txt_url.Focus();
                    return;
                }
                txt_kildekode.Clear();
                OpenURLInBrowser(txt_url.Text);
                GetLinksToTextbox();
            }
        }
      
        public void GetLinksToTextbox()
        {
            
            Crawler.Crawler c = new Crawler.Crawler(txt_url.Text);
            //int antal = c.GetNumberOfLinks(txt_url.Text);
            txt_kildekode.Text += "Udskriver links\n";
            foreach (Vertex v in c.BGraph.VertexList)
            {
                txt_kildekode.Text += v.Name + "\n";
            }
            txt_kildekode.Text += "\n";

            txt_kildekode.Text += "Udskriver matrix\n";
            for (int i = 0; i < c.BGraph.VertexList.Count; i++)
            {
                for (int j = 0; j < c.BGraph.VertexList.Count; j++)
                {
                    txt_kildekode.Text += c.BGraph.adjMatrix[i, j] + "   ";
                }
                txt_kildekode.Text += "\n";
            }
        }

        private void txt_kildekode_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            string url = e.LinkText;
            txt_url.Text = url;
            OpenURLInBrowser(txt_url.Text);
        }

        private void btn_søg_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txt_url.Text) || txt_url.Text.Equals("about:blank"))
            {
                MessageBox.Show("Enter a valid URL.");
                txt_url.Focus();
                return;
            }
            txt_kildekode.Clear();
            OpenURLInBrowser(txt_url.Text);
            GetLinksToTextbox();
        }
        private void OpenURLInBrowser(string url)
        {
            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
            {
                url = "http://" + url;
            }
            try
            {
                webBrowser1.Navigate(new Uri(url));
            }
            catch (System.UriFormatException)
            {
                return;
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

    }
}
